/****************************************************************
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
*set mem 2g;
drop _all;
set more 1;
pause on;

log using log_MSM_cf_figtbl.log, replace;

if (strpos("`c(pwd)'", "_pthealth") > 0) {;
    global gpth = 1;
    global gpicsdir "pics_pthealth_cf";
    global gcfn = 12;

    global gnoss 10;  // No SS, SSDI unchanged;
    global gnossb 12;  // remove SSB, SSDI unchanged;
    global greducessb 11;  // Reduce SS benefit by 20%, SSDI unchanged;
};
else {;
    global gpth = 0;
    global gpicsdir "pics_cf";
    global gcfn = 9;

    global gnoss 3;   // No SS;
    global gnossb 6;  // remove SSB;
    global greducessb 5;  // Reduce SS benefit by 20%;
};
global gAIMElast 26; //26---Modifying C5: AIME_t+1 = w_t (last year only);

global gAIMEn 28; // 28 is always lambda = 0.4518;
global gAIMEnVary VaryPIA${gAIMEn}r;

global gdtadir ".";

include do_globals.do;


/***********************************************
  Moments generated in Fortran program

local lvprofiles_all lfpr lfpr_ft lfpr_pt lfpr_eq labor inc lnw H I I_cond C 
                     p50A p50lnw p50lnw_fe p50H p50I p50I_cond p50C irecss ssa EV A retage;
************************************************/
if ((strpos("`c(pwd)'", "_lbd") > 0) | (strpos("`c(pwd)'", "_exo") > 0)) {;
    local lvlbdexo = 1;
    local lvprofiles_all lfpr lfpr_ft lfpr_pt lfpr_eq labor inc lnw H I C 
                         p50I ssa EV A retage;
    local lvxstat lfpr lfpr_ft lfpr_pt lfpr_eq lfpr_eq_sum labor inc lnw H I C p50I EV EVinit A;
};
else {;
    local lvlbdexo = 0;
    local lvprofiles_all lfpr lfpr_ft lfpr_pt lfpr_eq labor inc lnw H I I_cond C 
                         p50I p50I_cond ssa EV A retage;
    local lvxstat lfpr lfpr_ft lfpr_pt lfpr_eq lfpr_eq_sum labor inc lnw H I I_cond C p50I p50I_cond EV EVinit A;
};

local lvxsall t;
foreach iv in `lvprofiles_all' {;
   local lvxsall0 `lvxsall' `iv'*;
   local lvxsall `lvxsall0';
};

local lvx1 "";
foreach iv of local lvxstat {;
   local lvxstat_d `lvx1' d_`iv';
   local lvx1 `lvxstat_d';
};

*plot now;
use ${gdtadir}/dta_MSM_cf_Moments.dta if t>=18 & t<=80, clear;
keep `lvxsall';

foreach iv in `lvprofiles_all' {;
   if ("`iv'"=="lfpr") {;
      local lviv "Employment Rates";
      local lvpos = 11;
      local lvposlvl = 7;
   };
   else if ("`iv'"=="lfpr_pt") {;
      local lviv "ER (PT)";
      local lvpos = 12;
   };
   else if ("`iv'"=="lfpr_eq") {;
      local lviv "ER (FT Equivalent)";
      local lvpos = 11;
   };
   else if ("`iv'"=="lnw") {;
      local lviv "Log Wages";
      local lvpos = 11;
      if (${gpth}==1) {;
          local lvpos = 12;
      };
   };
   else if ("`iv'"=="H") {;
      local lviv "Human Capital";
      local lvpos = 11;
      local lvposlvl = 7;
   };
   else if ("`iv'"=="I" | "`iv'"=="I_cond") {;
      local lviv "Investment";
      local lvpos = 11;
      local lvposlvl = 2;
      if (${gpth}==1) {;
          local lvpos = 5;
      };
   };
   else if (inlist("`iv'", "irecss")) {;
      local lviv "SSA";
      local lvpos = 5;
   };
   else if (inlist("`iv'", "ssa")) {;
      local lviv "SSA";
      local lvpos = 2;
   };
   else if (inlist("`iv'", "retage")) {;
      local lviv "Retirement age distribution";
      local lvpos = 11;
   };
   else {;
      local lviv "`iv'";
      local lvpos = 11;
   };

   if (inlist("`iv'", "irecss", "ssa")) {;
       local lvxlabsca "xscale(range(60 70)) xlabel(60(1)70,grid)";
       local lvcond "if t>=60 & t<=70";
   };
   else if (inlist("`iv'", "retage")) {;
       local lvxlabsca "xscale(range(50 70)) xlabel(50(5)80,grid)";
       local lvcond "if t>=50 & t<=80";
   };
   else {;
       local lvxlabsca "xscale(range(18 ${gvNT})) xlabel(20(10)${gvNT},grid)";
       local lvcond "if t<=${gvNT}";
   };

   * plot the level;
    if (inlist("`iv'", "irecss")) {;
        local lvpos = 5;
    };
    else if (inlist("`iv'", "ssa")) {;
        local lviv "SSA";
        local lvpos = 2;
    };

   * difference now, for each age, so missing for 81-90;
   forvalues jv = 1/${gcfn} {;  // 9 or 12 (pthealth)
      gen d_`iv'`jv' = `iv'`jv' - `iv'0;
      gen pd_`iv'`jv' = 100 * d_`iv'`jv' / `iv'0;
   };
   forvalues jv = 20/28 {;  // AIME to PIA
      gen d_`iv'`jv' = `iv'`jv' - `iv'0;
      gen pd_`iv'`jv' = 100 * d_`iv'`jv' / `iv'0;
   };

   if ("`iv'"=="lfpr" | "`iv'"=="H" | "`iv'"=="lnw" | 
       (`lvlbdexo'==0 & ("`iv'"=="I" | "`iv'"=="I_cond"))) {;
       local lvdpd d_ pd_;
   };
   else {;
       local lvdpd d_;
   };
   *foreach jv in "d_" "pd_" {;
   foreach jv of local lvdpd  {;
      local lyange "";
      local lylabel "";
      if ("`jv'"=="d_") {;
         local lvjv "Difference in ";
         local lvtpct "";
      };
      else {;
         local lvjv "%Difference in ";
         local lvtpct %;
      };
      local lyrange_rb "";
      local lylabel_rb "";
      local lyrange_noss "";
      local lylabel_noss "";
      local lyrange_aime "";
      local lylabel_aime "";

    /***********************************************/
      if ("`jv'"=="d_") {;
         if ("`iv'"=="lfpr") {;
            local lyrange_rb -0.04 0.06;
            local lylabel_rb -0.04(0.02)0.06;
            local lyrange_noss -0.12 0.25;
            local lylabel_noss -0.1(0.1)0.2;
         };
         *else if ("`iv'"=="lnw") {;
         *   local lyrange -0.01 0.1;
         *   local lylabel -0.01 0(0.025)0.1;
         *};
         else if ("`iv'"=="I" | "`iv'"=="I_cond") {;
            local lyrange_rb -0.0025 0.01;
            local lylabel_rb -0.0025(0.0025)0.01;
            local lyrange_noss -0.01 0.02;
            local lylabel_noss -0.01(0.01)0.02;
            local lyrange_aime -0.01 0.02;
            local lylabel_aime -0.01(0.01)0.02;         
         };
         else if ("`iv'"=="H") {;
            local lyrange_aime -1.2 2.2;
            local lylabel_aime -1(1)2;
         };
         else if ("`iv'"=="lnw") {;
            local lyrange_aime -0.4 0.28;
            local lylabel_aime -0.4(0.2)0.2;
         };
         else if (inlist("`iv'", "irecss")) {;
             local lvpos = 2;
         };
         else if (inlist("`iv'", "ssa")) {;
             local lvpos = 5;
         };
      };
      else if ("`jv'"=="pd_") {;
         if ("`iv'"=="I" | "`iv'"=="I_cond") {;
            *local lyrange_rb -5 20;
            *local lylabel_rb -5(5)20;
            *local lyrange_noss -50 160;
            *local lylabel_noss -50(50)150;
         };
         else if ("`iv'"=="H") {;
            local lyrange_rb -2.5 10;
            local lylabel_rb -2.5 0(5)10;
            local lyrange_aime -20 35;
            local lylabel_aime -20(10)30;
         };
      };

      /***********************************************/

      /***
      line `jv'`iv'2 `jv'`iv'9 t `lvcond',
             lpattern("l" "_" "_." "-" "-." ".")  xtitle("Age") ytitle("`lvjv'`lviv'")
             `lvxlabsca' graphregion(color(white)) yscale(range(`lyrange')) ylabel(`lylabel')
             legend(pos(`lvpos') ring(0) col(1) colgap(*0.25) symxsize(*1) 
                    order(1 "NRA=67 NOT SSA" 2 "NRA=67"));
      graph export ${gpicsdir}/eps_cf_NRA67_`jv'`iv'.eps, replace;
      ****/

      line `jv'`iv'8 `jv'`iv'1 `jv'`iv'9 `jv'`iv'${greducessb} `jv'`iv'${gAIMEn} `jv'`iv'${gAIMElast} t `lvcond',
             lpattern("l" "_" "_." "-" "-." ".")  xtitle("Age") ytitle("`lvjv'`lviv'")
             `lvxlabsca' graphregion(color(white)) 
             title("`lvtpct'{&Delta}`lviv'", position(1) ring(0))
             yscale(range(`lyrange_rb')) ylabel(`lylabel_rb')
             legend(pos(`lvpos') ring(0) col(1) colgap(*0.25) symxsize(*1) 
                    order(1 "Increase tax 50%" 2 "No ET" 3 "NRA=67" 
                          4 "Reduce SS 20%" 5 "Linear PIA" 6 "AIME=last wage"));
      graph export ${gpicsdir}/eps_cf_reducebenefit_`jv'`iv'.eps, replace;

      if (strpos("`c(pwd)'", "_health") > 0) & ("`iv'"=="H") & ("`jv'"=="d_")  {;
          local lvpos_noss = 7;
      };
      else if (strpos("`c(pwd)'", "_health") > 0) & ("`iv'"=="lfpr") & ("`jv'"=="d_")  {;
          local lvpos_noss = 12;
      };
      else {;
          local lvpos_noss = `lvpos';
      };
      /**
      line `jv'`iv'${gnossb} `jv'`iv'${gAIMElast} t `lvcond',
             lpattern("l" "_" "_." "-" "-." ".")  xtitle("Age") ytitle("`lvjv'`lviv'")
             `lvxlabsca' graphregion(color(white))
             title("`lvtpct'{&Delta}`lviv'", position(1) ring(0))
             yscale(range(`lyrange_noss')) ylabel(`lylabel_noss')
             legend(pos(`lvpos_noss') ring(0) col(1) colgap(*0.25) symxsize(*1) 
                    order(1 "No SS benefit" 2 "AIME=last wage"));
      graph export ${gpicsdir}/eps_cf_ssbAIME_`jv'`iv'.eps, replace;
      **/
      
      line `jv'`iv'7 `jv'`iv'${gnossb} `jv'`iv'${gnoss} t `lvcond',
             lpattern("l" "_" "_." "-" "-." ".")  xtitle("Age") ytitle("`lvjv'`lviv'")
             `lvxlabsca' graphregion(color(white))
             title("`lvtpct'{&Delta}`lviv'", position(1) ring(0))
             yscale(range(`lyrange_noss')) ylabel(`lylabel_noss')
             legend(pos(`lvpos_noss') ring(0) col(1) colgap(*0.25) symxsize(*1) 
                    order(1 "No SS taxes" 2 "No SS benefit" 3 "No SS system"));
      graph export ${gpicsdir}/eps_cf_noss_`jv'`iv'.eps, replace;

      if (strpos("`iv'", "lfpr") > 0) & ("`jv'"=="d_")  {;
          local lvpos_aime = 11;
          local lyrange_aime -0.2 0.22;
          local lylabel_aime -0.2(0.1)0.2;
      };
      else if (strpos("`iv'", "lnw") > 0) & ("`jv'"=="d_")  {;
          local lvpos_aime = 7;
      };
      else {;
          local lvpos_aime = `lvpos';
      };
      line `jv'`iv'21 `jv'`iv'22 `jv'`iv'23 `jv'`iv'24 `jv'`iv'25 `jv'`iv'26 t `lvcond',
             lpattern("l" "_" "_-" "_." "-" "-." ".")  xtitle("Age") ytitle("`lvjv'`lviv'")
             `lvxlabsca' graphregion(color(white)) 
             title("`lvtpct'{&Delta}`lviv'", position(1) ring(0))
             yscale(range(`lyrange_aime')) ylabel(`lylabel_aime')
             legend(pos(`lvpos_aime') ring(0) col(1) colgap(*0.25) symxsize(*1) 
                    order(1 "PIA=0.1*AIME" 2 "PIA=0.3*AIME" 3 "PIA=0.5*AIME" 
                          4 "PIA=0.7*AIME" 5 "PIA=0.9*AIME" 6 "AIME=last wage"));
      graph export ${gpicsdir}/eps_cf_AIME_`jv'`iv'.eps, replace;


    /***
      line `jv'`iv'4 t `lvcond', lpattern("l" "_" "_." "-" "-." ".")  xtitle("Age") ytitle("`lvjv'`lviv'")
             `lvxlabsca' graphregion(color(white));
      graph export ${gpicsdir}/eps_cf_noshock_`jv'`iv'.eps, replace; 
    ****/
   };
};

* 1 "Increase tax 50%" 2 "No ET" 3 "NRA=67" 4 "Reduce SS 20%");
* 1 "No SS taxes" 2 "No SS benefit" 3 "No SS system");
export excel t d_lnw8 d_lnw1 d_lnw9 d_lnw${greducessb}
               d_lnw7 d_lnw${gnossb} d_lnw${gnoss}
               using "table4paper${alt_name}" if t>=50 & t<=80, sheet("cf_lnw${alt_name}") 
            sheetreplace firstrow(variables);

*cap log close;
*stop;

***************************************************************;
*  Now generate tables, aggregate over all ages;
****************************************************************;
local lvxsd "";
local lvxspd "";
foreach iv in `lvprofiles_all' {;
   local lvxs_d `lvxsd' d_`iv';
   local lvxsd `lvxs_d';
   local lvxs_pd `lvxspd' pd_`iv';
   local lvxspd `lvxs_pd';
};

* 99 is data moments;
drop *99;

sort t;

local lvxall `lvprofiles_all' `lvxs_d' `lvxs_pd';
reshape long `lvxall', i(t) j(cfid);

replace retage = retage * t;

gen lfpr_eq_sum = lfpr_eq;
gen d_lfpr_eq_sum = d_lfpr_eq;
gen pd_lfpr_eq_sum = pd_lfpr_eq;


foreach jv in "" "65" {;
    if ("`jv'"=="65") {;
        keep if t>=${gvdata0} & t<=${gvdata10};
    };
    sort cfid t;

    cap drop EVinit;
    by cfid: gen EV0 = EV if [_n]==1;
    by cfid: egen EV0min = min(EV0);
    drop EV0;
    ren EV0min EVinit;

    * level;
    preserve;
       collapse (mean) `lvxstat', by(cfid);
       order cfid `lvxstat';
       sort cfid;
       *pause;
       xpose, clear varname;
       *pause;
       gen itype = "mean";
       save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;
    restore;
    preserve;
       collapse (sum) `lvxstat' retage, by(cfid);
       order cfid `lvxstat' retage;
       sort cfid;
       xpose, clear varname;
       gen itype = "sum";
       append using ${gpicsdir}/dta_MSM_cf`jv'_table.dta;
       save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;
    restore;

    * difference;
    preserve;
       keep `lvxstat' cfid;
       collapse (mean) `lvxstat', by(cfid);
       sort cfid;
       foreach iv of local lvxstat {;
          gen `iv'_b = `iv' if cfid == 0;
          egen `iv'_base = min(`iv'_b);
          drop `iv'_b;
          replace `iv' = `iv'-`iv'_base;
          drop `iv'_base;
       };
       order cfid `lvxstat';
       sort cfid;
       xpose, clear varname;
       gen itype = "mean.d";
       append using ${gpicsdir}/dta_MSM_cf`jv'_table.dta;
       save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;
    restore;
    preserve;
       keep `lvxstat' retage cfid;
       collapse (sum) `lvxstat' retage, by(cfid);
       sort cfid;
       foreach iv in `lvxstat' retage {;
          gen `iv'_b = `iv' if cfid == 0;
          egen `iv'_base = min(`iv'_b);
          drop `iv'_b;
          replace `iv' = `iv'-`iv'_base;
          drop `iv'_base;
       };
       order cfid `lvxstat' retage;
       sort cfid;
       xpose, clear varname;
       gen itype = "sum.d";
       append using ${gpicsdir}/dta_MSM_cf`jv'_table.dta;
       save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;
    restore;

    * percent difference;
    preserve;
       keep `lvxstat' cfid;
       collapse (mean) `lvxstat', by(cfid);
       sort cfid;
       foreach iv of local lvxstat {;
          gen `iv'_b = `iv' if cfid == 0;
          egen `iv'_base = min(`iv'_b);
          drop `iv'_b;
          replace `iv' = 100 * (`iv'-`iv'_base) / `iv'_base;
          drop `iv'_base;
       };
       order cfid `lvxstat';
       sort cfid;
       xpose, clear varname;
       gen itype = "mean.pd";
       append using ${gpicsdir}/dta_MSM_cf`jv'_table.dta;
       save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;
    restore;
    preserve;
       keep `lvxstat' retage cfid;
       collapse (sum) `lvxstat' retage, by(cfid);
       sort cfid;
       foreach iv in `lvxstat' retage {;
          gen `iv'_b = `iv' if cfid == 0;
          egen `iv'_base = min(`iv'_b);
          drop `iv'_b;
          replace `iv' = 100 * (`iv'-`iv'_base) / `iv'_base;
          drop `iv'_base;
       };
       order cfid `lvxstat';
       sort cfid;
       xpose, clear varname;
       gen itype = "sum.pd";
       append using ${gpicsdir}/dta_MSM_cf`jv'_table.dta;
       save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;
    restore;
};

foreach jv in "" "65" {;

    use ${gpicsdir}/dta_MSM_cf`jv'_table.dta, clear;

    ren v1 baseline;
    ren v2 NoSSTest;
    ren v3 DelayNRAonly;  //2--Delaying Social Security Normal Retirement Age (NRA);
    ren v4 NoSSsystem;
    ren v5 NoUncertainty;
    ren v6 ReduceSSbenefit;
    ren v7 NoSSbenefit;
    ren v8 NoSStax;
    ren v9 IncreaseTax;
    ren v10 DelayNRAnUtil;  //9--Delaying NRA to 67 and change SSA utility accordingly;

    if (${gpth}==1) {;
        ren v11 NoSS_wSSDI;
        ren v12 ReduceSSb_wSSDI;
        ren v13 NoSSb_wSSDI;
        local lvexp0 ReduceSSb_wSSDI NoSSb_wSSDI NoSS_wSSDI;
        local lvcn = 14;
    };
    else {;
        local lvexp0 "";
        local lvcn = 11;
    };

    ren v`lvcn' VaryPIA061r;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryPIA01r;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryPIA03r;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryPIA05r;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryPIA07r;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryPIA09r;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryAIMElast;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';
    ren v`lvcn' VaryAIMElastMax;
    local lvcn0 = `lvcn'+1;
    local lvcn = `lvcn0';

    local lvVarys "";
    local lvVarys2 "";
    local lvVarys3 "";
    local lvVarys30 "";
    forvalues iv = 28/28 {;
       ren v`lvcn' VaryPIA`iv'r;
       local lvcn0 = `lvcn'+1;
       local lvcn = `lvcn0';
       local lvVarys_0 `lvVarys' VaryPIA`iv'r;
       local lvVarys `lvVarys_0';
       local lvVarys2_0 `lvVarys2' VaryPIA`iv'r2;
       local lvVarys2 `lvVarys2_0';
       local lvVarys3_0 `lvVarys3' VaryPIA`iv'r3;
       local lvVarys3 `lvVarys3_0';
       local lvVarys30_0 `lvVarys30' VaryPIA`iv'r30;
       local lvVarys30 `lvVarys30_0';
    };
    
    local lvexp IncreaseTax NoSSTest DelayNRAonly DelayNRAnUtil ReduceSSbenefit 
                NoSSbenefit NoSStax NoSSsystem NoUncertainty `lvexp0'
                VaryPIA01r VaryPIA03r VaryPIA05r VaryPIA07r VaryPIA09r 
                VaryAIMElast VaryPIA061r VaryAIMElastMax
                `lvVarys';

    order itype _varname baseline `lvexp';

    compress;
    save ${gpicsdir}/dta_MSM_cf`jv'_table.dta, replace;

    **************************************************;
    * make table directly for the paper;
    *drop if trim(itype) == "mean" | trim(itype) == "mean.d" | trim(itype) == "mean.pd";

    gen byte iid = 1 if trim(itype) == "sum";
    replace iid = 2 if trim(itype) == "sum.d";
    replace iid = 3 if trim(itype) == "sum.pd";

    replace iid = 10 if trim(itype) == "mean";
    replace iid = 20 if trim(itype) == "mean.d";
    replace iid = 30 if trim(itype) == "mean.pd";

    reshape wide itype baseline `lvexp', i(_varname) j(iid);

    gen ivar = 1 if trim(_varname) == "lfpr";
    local lvbase = 3;
    replace ivar = 20+`lvbase' if trim(_varname) == "labor";
    replace ivar = 30+`lvbase' if trim(_varname) == "inc";
    replace ivar = 40+`lvbase' if trim(_varname) == "lnw";
    replace ivar = 50+`lvbase' if trim(_varname) == "H";
    replace ivar = 60+`lvbase' if trim(_varname) == "I";
    replace ivar = 70+`lvbase' if trim(_varname) == "C";
    replace ivar = 80+`lvbase' if trim(_varname) == "EVinit";
    replace ivar = 90+`lvbase' if trim(_varname) == "EV";
    replace ivar = 100+`lvbase' if trim(_varname) == "retage";
    
    replace ivar = 0 if trim(_varname) == "cfid";


    if (${gpth}==1) {;
        replace ivar = 3 if trim(_varname) == "lfpr_eq_sum";
        replace ivar = 4 if trim(_varname) == "lfpr_eq";
        replace ivar = 6 if trim(_varname) == "lfpr_pt";
        replace ivar = 8 if trim(_varname) == "lfpr_ft";

        local lvextra ReduceSSb_wSSDI NoSSb_wSSDI NoSS_wSSDI;
        local lvextra23 ReduceSSb_wSSDI2 ReduceSSb_wSSDI3 NoSSb_wSSDI2 NoSSb_wSSDI3 NoSS_wSSDI2 NoSS_wSSDI3;
        local lvextra2030 ReduceSSb_wSSDI20 ReduceSSb_wSSDI30 NoSSb_wSSDI20 NoSSb_wSSDI30 NoSS_wSSDI20 NoSS_wSSDI30;
    };
    else {;
        local lvextra "";
        local lvextra23 "";
        local lvextra2030 "";
        *local lvextra NoUncertainty2 NoUncertainty3;
    };

    sort ivar;

    * use sum for lfpr (1) and retage (10);
    foreach iv in baseline1 IncreaseTax2 IncreaseTax3 NoSSTest2 NoSSTest3 
              DelayNRAonly2 DelayNRAonly3 DelayNRAnUtil2 DelayNRAnUtil3
              ReduceSSbenefit2 ReduceSSbenefit3 
              NoSStax2 NoSStax3 NoSSbenefit2 NoSSbenefit3
              NoSSsystem2 NoSSsystem3 `lvextra23' 
              VaryPIA01r2 VaryPIA01r3 
              VaryPIA03r2 VaryPIA03r3 VaryPIA05r2 VaryPIA05r3 VaryPIA07r2 VaryPIA07r3 
              VaryPIA09r2 VaryPIA09r3 VaryAIMElast2 VaryAIMElast3  
              `lvVarys2' `lvVarys3'  {;
           replace `iv'0 = `iv' if ivar==1 | ivar==3 | ivar==100+`lvbase'; 
           * 1 lfpr, 3 lfpr_eq_sum (with pthealth), 100+`lvbase': retage; 
    };
    * for EV (9) and EVinit (8), use the difference (2) instead of %difference (3);
    foreach iv in IncreaseTax NoSSTest 
              DelayNRAonly DelayNRAnUtil ReduceSSbenefit 
              NoSStax NoSSbenefit NoSSsystem `lvextra' 
              VaryPIA01r 
              VaryPIA03r VaryPIA05r VaryPIA07r 
              VaryPIA09r VaryAIMElast
              `lvVarys' {;
           replace `iv'3 = `iv'2 if ivar==80+`lvbase' | ivar==90+`lvbase'; 
           replace `iv'30 = `iv'20 if ivar==80+`lvbase' | ivar==90+`lvbase'; 
           * EV (90) and EVinit (80); 
    };

    * for the table in the paper;
    if (${gpth}==1) {;
        export excel _varname baseline10 IncreaseTax30 NoSSTest30 DelayNRAnUtil30
                  ReduceSSb_wSSDI30 ${gAIMEnVary}30 VaryAIMElast30 
                  NoSStax30 NoSSb_wSSDI30 NoSS_wSSDI30 
               using "table4paper${alt_name}", sheet("cf`jv'${alt_name}") 
                  sheetreplace firstrow(variables);
        export excel _varname baseline10 IncreaseTax20 NoSSTest20 DelayNRAnUtil20
                  ReduceSSb_wSSDI20 ${gAIMEnVary}20 VaryAIMElast20   
                  NoSStax20 NoSSb_wSSDI20 NoSS_wSSDI20 
               using "table4paper${alt_name}", sheet("cf_mean`jv'${alt_name}") 
                  sheetreplace firstrow(variables);
        export excel _varname baseline10 VaryPIA01r30 VaryPIA03r30 
                     VaryPIA05r30 VaryPIA07r30 VaryPIA09r30 
                  using "table4paper${alt_name}", sheet("cf`jv'${alt_name}_AIME") 
              sheetreplace firstrow(variables);          
    };
    else {;
        export excel _varname baseline10 IncreaseTax30 NoSSTest30 DelayNRAnUtil30
                  ReduceSSbenefit30 ${gAIMEnVary}30 VaryAIMElast30  
                  NoSStax30 NoSSbenefit30 NoSSsystem30 
               using "table4paper${alt_name}", sheet("cf`jv'${alt_name}") 
                  sheetreplace firstrow(variables);
        export excel _varname baseline10 IncreaseTax20 NoSSTest20 DelayNRAnUtil20
                  ReduceSSbenefit20 ${gAIMEnVary}20 VaryAIMElast20  
                  NoSStax20 NoSSbenefit20 NoSSsystem20 
               using "table4paper${alt_name}", sheet("cf_mean`jv'${alt_name}") 
                  sheetreplace firstrow(variables);
        export excel _varname baseline10 VaryPIA01r30 VaryPIA03r30 
                     VaryPIA05r30 VaryPIA07r30 VaryPIA09r30 
                  using "table4paper${alt_name}", sheet("cf`jv'${alt_name}_AIME") 
              sheetreplace firstrow(variables);          
    };
    export excel _varname baseline10 `lvVarys30'
       using "table4paper${alt_name}", sheet("cf`jv'${alt_name}_AIME2") 
          sheetreplace firstrow(variables);
};

cap log close;


